function IsImageLoadedNew(night) {
    var img = event.srcElement;
    if (img.src.indexOf("picture_place_holder.png") > 0 || img.src.indexOf("picture_place_holder_night.png") > 0) {
        if (night == "0") {
            var loading = "file:///android_asset/ruanmei_webview_loadingimage.png";
            img.src = loading;
        } else {
            var loading = "file:///android_asset/ruanmei_webview_loadingimage_night.png";
            img.src = loading;
        }
    } else if (img.complete) {
        var src = img.attributes['originsrc'].nodeValue;
        ProxyClickPicture.clickImg(src);
    }
}


function IsGifImageLoadedNew(night) {
    var img=event.srcElement;
    if (img.src.indexOf("picture_place_gif_holder.png") > 0 || img.src.indexOf("picture_place_holder_gif_night.png") > 0) {
        if (night == "0") {
            var loading = "file:///android_asset/ruanmei_webview_loadingimage.png";
            img.src = loading;
        } else {
            var loading = "file:///android_asset/ruanmei_webview_loadingimage_night.png";
            img.src = loading;
        }
    } else if (img.complete) {
         var src = img.attributes['originsrc'].nodeValue;
         ProxyClickPicture.clickImg(src);
     }
}

function imageload()
{
  var img=event.srcElement;

  if(img.src.indexOf("ruanmei_webview_loadingimage.png") > 0 || img.src.indexOf("ruanmei_webview_loadingimage_night.png") > 0)
  {
      img.src = img.attributes['originsrc'].nodeValue;
  }
  else
  {
  }
}

function IsImageLoaded()
{
    var img=event.srcElement;

    if(img.src.indexOf("picture_place_holder.png") > 0)
    {
      img.src = img.attributes['loadingsrc'].nodeValue;
    }
    else if(img.src.indexOf("ruanmei_webview_loadingimage.png") > 0)
    {
    }
    else
    {
       if(img.complete)
       {
           var imagesrc = img.attributes['originsrc'].nodeValue;
           ProxyClickPicture.clickImg(imagesrc);
       }
       else
       {
       }
    }
}

function IsGifImageLoaded()
{
    var img=event.srcElement;

    if(img.src.indexOf("picture_place_gif_holder.png") > 0)
    {
        img.src = img.attributes['loadingsrc'].nodeValue;
    }
    else if(img.src.indexOf("ruanmei_webview_loadingimage.png") > 0)
    {
    }
    else
    {
         if(img.complete)
         {
            //ios ���ͼƬʹ��
             var imagesrc = img.attributes['originsrc'].nodeValue;
             //document.location = imagesrc.replace("http://","ruanmeipic://");
             ProxyClickPicture.clickImg(imagesrc);
         }
         else
         {
             //δ�������ʱ���ݲ��Ŵ�ͼƬ���Ժ�����޸�
             //img.src = img.attributes['originsrc'].nodeValue;
         }
    }
}
  function IsImageLoaded1()
          {
              var img=event.srcElement;

              if(img.src.indexOf("picture_place_holder.png") > 0)
              {
                  img.src = img.attributes['loadingsrc'].nodeValue;
              }
              else if(img.src.indexOf("ruanmei_webview_loadingimage.png") > 0)
              {
              }
              else
              {
                   if(img.complete)
                   {
						//ios ���ͼƬʹ��
                       var imagesrc = img.attributes['originsrc'] !== undefined ? img.attributes['originsrc'].nodeValue : "";
                       //document.location = imagesrc.replace("http://","ruanmeipic://");
                   }
                   else
                   {
                       //δ�������ʱ���ݲ��Ŵ�ͼƬ���Ժ�����޸�
                       //img.src = img.attributes['originsrc'].nodeValue;
                   }
              }
          }

/*投票相关 2016-10-12*/
function submitVote(id, votetype) {
    var x = document.getElementsByName("checkbox" + id + "[]");
    var m = 0;
    var itemids = '';
    for (var i = 0; i < x.length; i++) {
        if (x[i].checked) {
            m++;
            itemids += x[i].value + ',';
        }
    }
    if (m == 0) {// 没有选项
        ProxyClickPicture.notifyVote(id, 'voteEmpty:' + votetype);
        return;
    }
    if (m > votetype) {// 选项数量超出限制
        ProxyClickPicture.notifyVote(id, 'voteError:' + votetype);
        return;
    }

    // 成功投票
    ProxyClickPicture.notifyVote(id, 'voteSuccess:' + id + ":" + itemids);
}

function scrollLazyLaod(scrollHeight){
    $("img.lazy").each(function () {
        var clientH = window.screen.availHeight;
        var off = $(this).offset().top;
        var val = $(this).attr("data-original");
        if (off - scrollHeight < clientH) {
            $(this).attr("src", val);
            $(this).removeClass("lazy");
        }
    })
}


function swipeCheck(touchYDP){
    var allIframeElement = document.getElementsByTagName("iframe");
    var allPreElement = document.getElementsByTagName("pre");
    var canSwipe = checkIfNotInElements(allIframeElement, touchYDP) && checkNotInScrollablePres(allPreElement, touchYDP);
    ProxyClickPicture.swipeCheck(canSwipe);
}

function checkIfInElement(anchor, touchYDP){
    var top = anchor.offsetTop;
    var parent = anchor.offsetParent;
    while(parent!==null){
        top += parent.offsetTop;
        parent = parent.offsetParent;
    }
    var bottom = top+anchor.offsetHeight;
    return touchYDP >=top && touchYDP <= bottom;
}

function checkIfNotInElements(elements, touchYDP){
    var alenght = elements.length;
    var notIn = true;
    if(alenght > 0){
        for(var i=0; i < alenght; i++){
            var anchor = elements[i];
            if(checkIfInElement(anchor, touchYDP)){
                notIn = false;
            }
        }
    }
    return notIn;
}

function checkIfCanScrollHorizon(elem){
    var elem_child = elem.childNodes;
    var canScrollHorizon = false;
    for(var i=0; i<elem_child.length; i++){
        if(elem_child[i].offsetWidth > elem.offsetWidth){
            canScrollHorizon = true;
        }
    }
    return canScrollHorizon;
}

function checkNotInScrollablePres(elements, touchYDP){
    var alenght = elements.length;
    var canScroll = false;
    if(alenght > 0){
        for(var i=0; i < alenght; i++){
            var anchor = elements[i];
            if(checkIfInElement(anchor, touchYDP) && checkIfCanScrollHorizon(anchor)){
                canScroll = true;
            }
        }
    }
    return !canScroll;
}
function loadErrorImage(){
    var img=event.srcElement;
    img.src = img.attributes['data-errorImg'].nodeValue;
}
(function($){

    //a标签包裹的img不响应点击事件
    $("a img").removeAttr("onclick");

    //辣品链接、特定平台链接识别
    $("body").on('click', 'a', function(){
        if($(this).hasClass('lplink')){
            //辣品卡片链接
            var lapinProductId = $(this).attr("data-id");//商品ID
            var appDownloadUrl = $(this).attr("data-downurl");//app下载地址
        	if(typeof lapinProductId !== "undefined" && lapinProductId.length > 0){//判断ID已定义且不为空
        		event.preventDefault();//阻止a标签默认跳转行为
                //自行实现辣品app内打开商品：
                //  1、如果能跳转到辣品app内对应的商品页则跳转
                //  2、如果不能跳转到辣品app内对应商品页则在内置浏览器中打开下载页
        		ProxyClickPicture.showDetailInLapinApp(lapinProductId, appDownloadUrl);//需要自己实现的代码
        	}
        }else{
            //检查是否有平台特定链接
            var hrefLink = $(this).attr("href");//常规链接
            var iOSLink = $(this).attr("data-iosurl");//iOS链接
            var androidLink = $(this).attr("data-androidurl");//安卓链接
            var uwpLink = $(this).attr("data-uwpurl");//uwp链接
            var appLink = $(this).attr("data-appurl");//app通用链接

            var jdProductId = $(this).attr("data-jdid");//京东商品ID，注意判断不存在时，设置为空字符串
            if (typeof jdProductId == "undefined"){ jdProductId = ''; }

            if(typeof androidLink !== "undefined"  && androidLink.length > 0){
                event.preventDefault();
                ProxyClickPicture.navigateTo(androidLink, jdProductId);
            } else if (typeof appLink !== "undefined"  && appLink.length > 0){
                event.preventDefault();
                ProxyClickPicture.navigateTo(appLink, jdProductId);
            } else if (typeof jdProductId !== "undefined"  && jdProductId.length > 0){
                event.preventDefault();
                ProxyClickPicture.navigateTo(hrefLink, jdProductId);
            }
        }
    });

//    $("body").on('click', ".user_avatar", function(){
//        var userId = $(this).attr("data-userid");//用户ID
//        if(typeof userId !== "undefined" && userId.length > 0){
//            window.open("ithome://userpage?id=" + userId);
//        }
//    });
})(jQuery);